/*
 * @Author: Aaron亚伦
 * @Wechat: a1319com
 * @Date: 2020-04-15 09:47:20
 * @FilePath: /golang/pachong/myimg.go
 * @Version: 1.0.0
 * @LastEditors: Aaron亚伦
 * @LastEditTime: 2020-04-15 14:11:46
 */
package main

import (
	"fmt"
	"io/ioutil"
	"math/rand"
	"net/http"
	"os"
	"regexp"
	"strconv"
	"sync"
	"time"
	// "golang.org/x/text/encoding/simplifiedchinese"
	// "golang.org/x/text/transform"
)

var (
	// 图片规则
	reImg = `<img[\s\S]+?src="(http[\s\S]+?)"`

	//并发数量
	chSem      = make(chan int, 5)
	downloadWG sync.WaitGroup

	//加锁
	randomMT sync.Mutex
)

// //gbk转utf8
// func GbkToUtf8(s []byte) ([]byte, error) {

// 	reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewDecoder())
// 	d, e := ioutil.ReadAll(reader)
// 	if e != nil {
// 		return nil, e
// 	}
// 	return d, nil
// }

func HandErr(err error, when string) {
	if err != nil {
		fmt.Println(when, err)
		os.Exit(1)
	}
}

//获取页面html
func Gethtml(url string) string {
	resp, err := http.Get(url)
	HandErr(err, `http.Get`)
	// resp = GbkToUtf8(resp)
	defer resp.Body.Close()
	bytes, _ := ioutil.ReadAll(resp.Body)
	html := string(bytes)
	return html

}

//获取图片集
func GetPageImgurls(url string) []string {
	html := Gethtml(url)
	// fmt.Println(html)
	re := regexp.MustCompile(reImg)
	rets := re.FindAllStringSubmatch(html, -1)
	imgUrls := make([]string, 0)
	for _, ret := range rets {
		imgUrl := ret[1]
		imgUrls = append(imgUrls, imgUrl)
	}
	return imgUrls
}

//随机数 不包含 start,end
func GetRandomInt(start, end int) int {
	randomMT.Lock()
	<-time.After(1 * time.Nanosecond)
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	ret := start + r.Intn(end-start)
	randomMT.Unlock()
	return ret
}

//生成随机文件名
func GetRandName() string {
	timestamp := strconv.Itoa(int(time.Now().UnixNano()))
	randNum := strconv.Itoa(GetRandomInt(100, 1000))
	return timestamp + randNum
}

/*
  同步下载
*/
func DownloadImg(url string) {
	resp, _ := http.Get(url)
	defer resp.Body.Close()
	imgBytes, _ := ioutil.ReadAll(resp.Body)
	filename := `/Users/aaron/Files/golang/goa1319com/pachong/imgs/` + GetRandName() + ".jpg"
	err := ioutil.WriteFile(filename, imgBytes, 0644)
	if err == nil {
		fmt.Println(filename, "下载成功")
	} else {
		fmt.Println(filename, "下载失败")
	}
}

// 异步下载
func DownloadImgAsync(url string) {
	downloadWG.Add(1)
	go func() {
		chSem <- 123
		DownloadImg(url)
		<-chSem
		downloadWG.Done()
	}()
	downloadWG.Wait()
}

func main() {
	imgurls := GetPageImgurls("https://www.qq.com")
	for _, iu := range imgurls {
		fmt.Println(iu)
		// DownloadImg(iu)  //同步下载
		DownloadImgAsync(iu) //异步下载
	}
}
